/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrderBottom = function (root) {
    if(root === null) return []

    let res = [],
        levelArr,
        queue = [root];

    while (queue.length > 0) {
        let len = queue.length,
            levelArr = [],
            node;

        for (let i = 0; i < len; i++) {
            node = queue.shift()
            levelArr.push(node.val)
            node.left && queue.push(node.left)
            node.right && queue.push(node.right)
        }

        res.unshift(levelArr)
    }

    return res
};